import en_US from 'ant-design-vue/es/locale/en_US';
import th_TH from 'ant-design-vue/es/locale/th_TH';

import cs_CZ from 'ant-design-vue/es/locale/cs_CZ';
import da_DK from 'ant-design-vue/es/locale/da_DK';
import de_DE from 'ant-design-vue/es/locale/de_DE';
import es_ES from 'ant-design-vue/es/locale/es_ES';
import fr_FR from 'ant-design-vue/es/locale/fr_FR';
import hu_HU from 'ant-design-vue/es/locale/hu_HU';
import it_IT from 'ant-design-vue/es/locale/it_IT';
import ja_JP from 'ant-design-vue/es/locale/ja_JP';
import ko_KR from 'ant-design-vue/es/locale/ko_KR';
import nl_NL from 'ant-design-vue/es/locale/nl_NL';
import pl_PL from 'ant-design-vue/es/locale/pl_PL';
import pt_PT from 'ant-design-vue/es/locale/pt_PT';
import ru_RU from 'ant-design-vue/es/locale/ru_RU';
import sv_SE from 'ant-design-vue/es/locale/sv_SE';
import tr_TR from 'ant-design-vue/es/locale/tr_TR';
import zh_CN from 'ant-design-vue/es/locale/zh_CN';

import { createI18n } from 'vue-i18n';
import { langOptions } from './data/options';
import { ExcelUtils } from './utils/file/Excel';
export const langMap: any = {
  中文: zh_CN,
  index: en_US,
  Español: es_ES,
  Français: fr_FR,
  Deutsch: de_DE,
  日本語: ja_JP,
  한국어: ko_KR,
  Italiano: it_IT,
  Português: pt_PT,
  Русский: ru_RU,
  Nederlands: nl_NL,
  Svenska: sv_SE,
  Dansk: da_DK,
  Polski: pl_PL,
  Čeština: cs_CZ,
  Magyar: hu_HU,
  Türkçe: tr_TR,
  ไทย: th_TH,
};
// 创建 i18n 对象
const i18n = createI18n({
  legacy: false,
  globalInjection: true, // 全局模式，可以直接使用 $t
  locale: localStorage.getItem('lang') || 'zh',
  messages: {
    // zh,
    // en,
    // th,
  },
});
export const fetchTranslateFile = async () => {
  try {
    const jsonData = await ExcelUtils.readExcelFromUrl('/translate.xlsx');
    console.log('翻译数据加载成功:', jsonData);

    // 遍历从Excel获取的每种语言数据
    for (const obj in jsonData) {
      let lang = '';
      langOptions.forEach(v => {
        if (v.label === obj) {
          lang = v.value;
        }
      });

      if (jsonData.hasOwnProperty(obj)) {
        // 获取当前语言的翻译数据
        const translations: any = jsonData[obj];

        // 如果i18n实例中已经有该语言的基础配置，则合并数据
        if (i18n.global.availableLocales.includes(lang as any)) {
          const existingMessages = i18n.global.getLocaleMessage(lang);

          i18n.global.setLocaleMessage(lang, {
            ...existingMessages,
            ...translations,
          });
        } else {
          const hasAttrs = Object.keys(translations).length > 0;
          if (hasAttrs) {
            // 如果是新语言，则直接设置
            i18n.global.setLocaleMessage(lang, translations);
            // 添加到可用语言列表
            i18n.global.availableLocales.push(lang as any);
          }
        }
      }
    }
  } catch (error) {
    console.error('加载翻译文件时出错:', error);
    // 可以在这里添加错误处理逻辑，如显示错误提示
  }
};
export default i18n;
